﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace Entities
{
    public class ConnectionInformation
    {
        /// <summary>
        /// ServerName property can contain either name or IP address of the server.
        /// </summary>
        public string ServerAddress { set; get; }
        /// <summary>
        /// Type of Server or DataBase.
        /// </summary>
        public DataBaseType DataBaseType { set; get; }
        /// <summary>
        /// Enumerater Table or view.
        /// </summary>
        public DataBaseObject DataBaseObject { set; get; }
        /// <summary>
        /// Port number for server specific.
        /// </summary>
        public string Port { set; get; }
        /// <summary>
        /// Catalog Name or DataBase Name to which user wants to connect.
        /// </summary>
        public string DataBaseName { set; get; }
        /// <summary>
        /// Name of the authorized user.
        /// </summary>
        public string UserName { set; private get; }
        /// <summary>
        /// Password of user entered in UserName
        /// </summary>
        public string Password { set; private get; }

        /// <summary>
        /// User authentication mode, 'Windows' for windows integrated authentication mode and 'SQLServer' for SQL Server authentication mode.
        /// </summary>
        public AuthenticationMode AuthenticationMode { get; set; }
        /// <summary>
        /// Error message property having private set and public get method.
        /// </summary>
        public string ErrorMessage { private set; get; }
        /// <summary>
        /// This Property contains Connection String on the basis of authentication mode.
        /// </summary>

        //private string connectionString;
        //public string ConnectionString
        //{


        public String getConnectionString()
        {
            StringBuilder SB = new StringBuilder();
            StringBuilder ExceptionSB = new StringBuilder();
                try
                {
                    #region MSSQL Server
                    if (DataBaseType == DataBaseType.MSSQLServer && AuthenticationMode == AuthenticationMode.MSSQLServer)
                    {
                        if (string.IsNullOrEmpty(ServerAddress))
                            ExceptionSB.Append("Server Address not set.\n");
                        if (string.IsNullOrEmpty(UserName))
                            ExceptionSB.Append("User Name not set.\n");
                        if (string.IsNullOrEmpty(Password))
                            ExceptionSB.Append("Password not set.\n");

                        if (ExceptionSB.ToString().Length != 0)
                            throw (new Exception(ExceptionSB.ToString()));


                        SB.Append("Provider=SQLOLEDB; Data Source=");
                        SB.Append(ServerAddress);

                        if (!string.IsNullOrEmpty(DataBaseName))
                        {
                            SB.Append("; Initial Catalog=");
                            SB.Append(DataBaseName);
                        }

                        SB.Append("; User ID=");
                        SB.Append(UserName);

                        SB.Append("; Password=");

                        SB.Append(Password);

                        //connectionString = SB.ToString();

                    }
                    else if(DataBaseType == DataBaseType.MSSQLServer && AuthenticationMode == AuthenticationMode.MSSQLWindows)
                    {
                        if (string.IsNullOrEmpty(ServerAddress))
                            ExceptionSB.Append("Server Address not set.\n");

                        if (ExceptionSB.ToString().Length != 0)
                            throw (new Exception(ExceptionSB.ToString()));

                        SB.Append("Provider=SQLOLEDB; Data Source=");
                        SB.Append(ServerAddress);
                        SB.Append("; Trusted_Connection=Yes");

                        //connectionString = SB.ToString();
                    }
                    #endregion

                    #region MSAccess Database
                    if (DataBaseType == DataBaseType.MSAccess  && AuthenticationMode == AuthenticationMode.MSAccessWithPassword)
                    {
                        if (string.IsNullOrEmpty(ServerAddress))
                            ExceptionSB.Append("Please select a MS Access file.\n");

                        if (string.IsNullOrEmpty(Password))
                            ExceptionSB.Append("Password not set.\n");

                        if (ExceptionSB.ToString().Length != 0)
                            throw (new Exception(ExceptionSB.ToString()));

                        
                        //Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;
                        SB.Append("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=");
                        SB.Append(ServerAddress);

                        SB.Append(";Jet OLEDB:Database Password=");

                        SB.Append(Password);

                    }
                    else if (DataBaseType == DataBaseType.MSAccess && AuthenticationMode == AuthenticationMode.MSAccessWithoutPassword)
                    {
                        if (string.IsNullOrEmpty(ServerAddress))
                            ExceptionSB.Append("Please select a MS Access file.\n");
                        if (ExceptionSB.ToString().Length != 0)
                            throw (new Exception(ExceptionSB.ToString()));

                        //Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
                        SB.Append("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=");
                        SB.Append(ServerAddress);
                        SB.Append(";User Id=admin;Password=;");

                    }
                    #endregion

                    #region MySQL Server
                    if (DataBaseType == DataBaseType.MySQL && AuthenticationMode==AuthenticationMode.MySqlWithPassword)
                    {
                        if (string.IsNullOrEmpty(ServerAddress))
                            ExceptionSB.Append("Server Address not set.\n");
                        if (string.IsNullOrEmpty(UserName))
                            ExceptionSB.Append("User Name not set.\n");
                        if (string.IsNullOrEmpty(Password))
                            ExceptionSB.Append("Password not set.\n");

                        if (ExceptionSB.ToString().Length != 0)
                            throw (new Exception(ExceptionSB.ToString()));

                        SB.Append("SERVER=");
                        SB.Append(ServerAddress);
                        SB.Append("; DATABASE=");
                        if (!string.IsNullOrEmpty(DataBaseName))
                        {
                            SB.Append(DataBaseName);
                        }
                        else
                        {
                            SB.Append("TEST");
                        }

                        SB.Append("; UID=");
                        SB.Append(UserName);

                        SB.Append("; PASSWORD=");

                        SB.Append(Password);

                    }
                    else if (DataBaseType == DataBaseType.MySQL && AuthenticationMode == AuthenticationMode.MySqlWithoutPassword)
                    {
                        if (string.IsNullOrEmpty(ServerAddress))
                            ExceptionSB.Append("Server Address not set.\n");

                        if (ExceptionSB.ToString().Length != 0)
                            throw (new Exception(ExceptionSB.ToString()));

                        SB.Append("SERVER=");
                        SB.Append(ServerAddress);
                        SB.Append("; DATABASE=");

                        if (!string.IsNullOrEmpty(DataBaseName))
                        {
                            SB.Append(DataBaseName);
                        }
                        else
                        {
                            SB.Append("TEST");
                        }

                        SB.Append("; UID=root; PASSWORD=root;");

                        //connectionString = SB.ToString();
                    }
                    #endregion

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            return SB.ToString();
        }
    }
}
